<?php
/**
 * This file contains the class RegisterEmailModel which provides
 * most of the code requirements for the register email page
 *
 * @copyright  2009 A Memory Tree
 * @license    A Memory Tree License
 */
/**
 * This class provides the code for the register email page
 *
 * @copyright  2009 A Memory Tree
 * @license    A Memory Tree License
 */
class RegisterEmailModel
{
    /**
     * Access to the DB methods
     *
     * @var RegisterEmailDB
     */
    private $_db;
    /**
     * Access to the View methods
     *
     * @var RegisterEmailView
     */
    private $_view;
    /**
     * Access to the Authorisation methods
     *
     * @var AuthorisationModel
     */
    private $_auth;
    /**
     * Access to the Email methods
     *
     * @var EmailModel
     */
    private $_emailModel;
    /**
     * Access to the Server environment variables
     *
     * @var array[string]string
     */
    private $_server;
    /**
     * Access to the Helper methods
     *
     * @var Helper
     */
    private $_helper;
    /**
     * Create a new instance of this class
     *
     * @return void
     * @param RegisterEmailDB $db
     * @param RegisterEmailView $view
     * @param AuthorisationModel $auth
     * @param EmailModel $emailModel
     * @param array[string]string &$server
     * @param Helper $helper
     */
    function __construct($db, $view, $auth, $emailModel, &$server, $helper)
    {
        $this->_db = $db;
        $this->_view = $view;
        $this->_auth = $auth;
        $this->_emailModel = $emailModel;
        $this->_server = $server;
        $this->_helper = $helper;
    }
    /**
     * Check the email address for common errors
     *
     * @return string
     * @param string $email
     * @param string $emailConfirm
     */
    function emailError($email, $emailConfirm)
    {
        $view = $this->_view;
        $error = '';
        if (empty($email) && empty($emailConfirm)) {
            $error = 'emptyEmail';
        } else if ($email !== $emailConfirm) {
            $error = 'differentEmail';
        }
        return $error;
    }
    /**
     * Add a user to the database
     *
     * @return int
     * @param string $email
    */
    function addUser($email)
    {
        $db = $this->_db;
        $userId = 0;
        $existingUserId = $db->getUserIdByEmail($email);
        if (empty($existingUserId)) {
            $userId = $db->addUser($email);
        }
        return $userId;
    }
    /**
     * Create an authorisation code
     *
     * @return string
     * @param int $userId
     */
    function createAuth($userId)
    {
        $auth = $this->_auth;
        $authCode = $auth->createAuthCode($userId);
        return $authCode;
    }
    /**
     * Send activation email to the user
     *
     * @return bool
     * @param string $email
     * @param int $userId
     * @param string $authCode
     */
    function sendAuthEmail($email, $userId, $authCode)
    {
        $emailModel = $this->_emailModel;
        $view = $this->_view;
        $subject = $view->text('emailSubject');
        $message = $view->emailMessage($email, $userId, $authCode);
        $emailModel->setToEmail($email);
        $emailModel->setSubject($subject);
        $emailModel->setMessage($message);
        return $emailModel->send();
    }
    /**
     * Process the users email and render appropriate messages
     *
     * @return string
     * @param string $email
     */
    function processUser($email)
    {
        $db = $this->_db;
        $view = $this->_view;
        $content = '';
        $userId = $this->addUser($email);
        if (!empty($userId)) {
            $authCode = $this->createAuth($userId);
            $emailSent = $this->sendAuthEmail($email, $userId, $authCode);
            if ($emailSent) {
                $content = $view->content($email);
            } else {
                $content = $view->emailFailure();
            }
        } else {
            $userId = $db->getUserIdByEmail($email);
            $content = $view->duplicateUser($email, $userId);
        }
        return $view->render($content);
    }
    /**
     * If the visitor came from a previous page, provide page message
     *
     * @return string
     */
    function previousPage()
    {
        $view = $this->_view;
        $server = $this->_server;
        $previousPage = '';
        if (!empty($server['HTTP_REFERER'])) {
            $previousPage = $view->previousPage($server['HTTP_REFERER']);
        }
        return $previousPage;
    }
    /**
     * Provide an error message and if applicable, link to the previous page.
     *
     * @return string
     * @param string $errorType
     */
    function processError($errorType)
    {
        $view = $this->_view;
        $message = $view->text($errorType);
        $previousPage = $this->previousPage();
        return $view->error($message, $previousPage);
    }
    /**
     * Render the page
     *
     * @return string
     * @param RegisterEmailView &$view
     */
    function render()
    {
        $helper = $this->_helper;
        $content = '';
        $email = $helper->post('email');
        $emailConfirm = $helper->post('emailConfirm');
        $emailError = $this->emailError($email, $emailConfirm);
        if (empty($emailError)) {
            $content = $this->processUser($email);
        } else {
            $content = $this->processError($emailError);
        }
        return $content;
    }
}
?>